<?php

/**
 * Ext4Yii class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftware B.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
/**
 * Ext4Yii definitions
 */
defined('EXT4YII_PATH') or define('EXT4YII_PATH', dirname(__FILE__));
defined('EXT4YII_MSG') or define('EXT4YII_MSG', 'ext4yii_msg');

/**
 * <h2>Installation</h2>
 * The Ext4Yii class is the extension module class which has to registered in the
 * <b>protected/config/main.php</b> file in order to start Ext4Yii be recognized by the
 * Yii Framework.
 * To register the Ext4Yii Framework extension use the following example
 * <ext:Code brush="php">
 * // in protected/config/main.php
 * return array(
 *     'name' => 'My Application',
 *     ......
 *     // preloading 'log' and 'ext4yii' components
 *     'preload' => array('log','ext4yii'),
 *     // Add Ext4Yii to your application components array
 *     'components' => array(
 *         'ext4yii'=>array(
 *             'class' => 'application.extensions.ext4yii.Ext4Yii'
 *         ),
 *      .....
 * </ext:Code>
 * <p>The Framework parser generates readable JavaScript code when the Yii application is
 * running in DEBUG mode.</p>
 * <p>The Framework parser will pack the generated JavaScript code when the application is
 * running in normal mode without DEBUGGING being enabled.</p>
 * <h2>Disabling CSS reset</h2>
 * <p>By default the ExtJS Framework resets all CSS styles to a minimal set of configuration
 * to make the components render correctly. It is possible to disable the CSS reset by setting
 * the following property:</p>
 * <ext:Code brush="php">
 * class MyController extends ExtController
 * {
 *      public function init()
 *      {
 *          Yii::app()->ext4yii->disable_css_reset = true;
 *          parent::init();;
 *      }
 * }
 * </ext:Code>
 * <b>Disabling the CSS reset may cause the ExtJS components render incorrectly.</b>
 */
class Ext4Yii extends CApplicationComponent
{
    public $disable_pack = false;
    public $disable_css_reset = false;
    public $has_gd_support = false;
    public $packages = array();
    private $_ext4yiiCorePackages = array(
        'app.*', 'web.Core.*', 'web.Core.System.*', 'i18n.*',
        'web.Core.UI.*', 'web.Core.Shared.*', 'web.Core.Data.*',
        'web.Base.*', 'web.Base.Etc.*', 'web.Base.Shared.*', 'web.Base.Tree.*',
        'web.Base.Viewport.*', 'web.Base.Layout.*', 'web.Base.Panel.*',
        'web.Base.Button.*', 'web.Base.Toolbar.*', 'web.Base.Event.*',
        'web.Base.Date.*', 'web.Base.Data.*', 'web.Base.DataView.*',
        'web.Base.Form.*', 'web.Base.Grid.*', 'web.Base.List.*',
        'web.Base.Menu.*', 'web.Base.Window.*', 'web.Base.Yii.*',
        'web.Base.Tab.*', 'web.Ext.*', 'web.Ext.util.*', 'web.Ext.layout.*',
        'web.Ext.layout.BorderLayout.*', 'web.Ext.tree.*', 'web.Ext.Toolbar.*',
        'web.Ext.data.*', 'web.Ext.grid.*', 'web.Ext.list.*', 'web.Ext.menu.*',
        'web.Ext.form.*', 'web.Ext.ux.form.*', 'web.Ext.ux.grid.*', 'web.Ext.ux.*'
    );

    /**
     * @view
     * Show the current version of the Ext4Yii Framework extension.
     * @return string the version of Ext4Yii framework.
     */
    public static function getVersion()
    {
        return file_get_contents(EXT4YII_PATH . DIRECTORY_SEPARATOR . 'VERSION');
    }

    /**
     * Initializer responsible for loading Ext4Yii message and translation handling
     */
    public function init()
    {
        $this->checkV6();
        $this->importCorePackages();
        Yii::app()->setComponent(EXT4YII_MSG, new Ext4YiiMessageSource());
        Yii::app()->getComponent(EXT4YII_MSG);
        parent::init();
        $result = array();

        foreach ($this->packages as $packageClass)
        {
            $package = Yii::createComponent($packageClass);
            $package->init();
            $result[$package->getName()] = $package;
        }
        $this->packages = $result;

        if (extension_loaded('gd') && function_exists('gd_info'))
        {
            $this->has_gd_support = true;
        }
        else
        {
            $this->has_gd_support = false;
        }
    }

    /**
     * Translation function override for Ext4Yii
     */
    public static function t($category, $message, $params=array(), $source=EXT4YII_MSG, $language=null)
    {
        return Yii::t($category, $message, $params, $source, $language);
    }

    /**
     * Imports the default core packages
     */
    public function importCorePackages()
    {
        Yii::setPathOfAlias('ext4yii', EXT4YII_PATH);
        foreach ($this->_ext4yiiCorePackages as $package)
            Yii::import('ext4yii.' . $package);
    }

    private function checkV6()
    {
        if(defined('YII_EXT4YII_PATH'))
        {
            throw new Exception(Ext4Yii::t('ext4yii',"Unable to continue. Version {version} of the Ext4Yii Framework cannot coexist with version 0.6.\nPlease read the documentation for upgrading.",
                    array('{version}' =>  Ext4Yii::getVersion())
                    ));
        }
    }
}
?>
